package dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatCheckBox;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

public class ChooseDeviceDialog extends BaseDialog implements View.OnClickListener {

    private List<String> mSelectedDevices;

    public static ChooseDeviceDialog newInstance(ArrayList<String> devices) {
        ChooseDeviceDialog dialog = new ChooseDeviceDialog();
        Bundle arguments = new Bundle();
        arguments.putStringArrayList("devices", devices);
        dialog.setArguments(arguments);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        return inflater.inflate(R.layout.dialog_flow_choose_device, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.iv_close).setOnClickListener(v -> dismiss());

        mSelectedDevices = new ArrayList<>();

        TextView selectedCount = view.findViewById(R.id.selected_count);

        ViewGroup deviceContainer = view.findViewById(R.id.device_container);
        List<String> devices = new ArrayList<>();
        if (getArguments() != null) {
            ArrayList<String> temp = getArguments().getStringArrayList("devices");
            if (temp != null) {
                devices.addAll(temp);
            }
        }

        for (String number : devices) {
            View inflate = LayoutInflater.from(view.getContext()).inflate(R.layout.item_choose_device_list, null);
            TextView deviceNo = inflate.findViewById(R.id.device_no);
            deviceNo.setText(number);
            AppCompatCheckBox cb = inflate.findViewById(R.id.cb);
            cb.setOnCheckedChangeListener((compoundButton, b) -> {
                if (b && mSelectedDevices.indexOf(number) < 0) {
                    mSelectedDevices.add(number);
                } else {
                    mSelectedDevices.remove(number);
                }
                selectedCount.setText("已选择终端数量：" + mSelectedDevices.size());
            });

            deviceContainer.addView(inflate);
        }

        view.findViewById(R.id.bottom_layout).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bottom_layout:
                if (mSelectedDevices.size() == 0) {
                    Toast.makeText(getContext(), "请选择至少一台终端", Toast.LENGTH_SHORT).show();
                    return;
                }
                dismiss();
//                EventBus.getDefault().post(new SelectFlowPartnerEvent(mSelectedDevices));
                break;
        }
    }
}
